<?php
class UserController extends CF_Controller_Action{

	//const SORT_PARAM_NAME = 'username';
	//const SORT_PARAM_ROLE = 'role';
	const SORT_PARAM_SEARCH = 'search';
	const SORT_PARAM = 'sort';
	const ITEM_COUNT_PER_PAGE = 3;

	public function indexAction(){

		$sortSearchValue = $this->_getSortParams(self::SORT_PARAM_SEARCH);

		$sortFields = $this->_getSortParams(self::SORT_PARAM);
		$users = new Project_User_Table();

		//$this->view->user = $users->getUser(1);
		$pages = new Project_User_Table();
		$page = (int)$this->getRequest()->getParam('page',1);
		$this->view->paginatorUsers = $pages->getPaginatorUsers($page, $sortFields, $sortSearchValue, self::ITEM_COUNT_PER_PAGE);
		$this->view->PageStartIterator = ($page - 1) * self::ITEM_COUNT_PER_PAGE;
		$form = new Application_Form_Search();
		if(!empty($sortSearchValue)){
			$form->getElement('search')->setValue($sortSearchValue);
		}
		$this->view->title = 'User';
		$this->view->form  = $form;
		$this->view->sortFields = $sortFields;

	}
	//-----------!!!!!!!! better put it into the action helper !!!!!!!!!!!!!!!!!!

	private function _getSortParams($key)
	{
		$sortParams = $this->getRequest()->getParam($key);

		if(empty($sortParams)){
			return false;
		}
		return $this->_cleanParams($sortParams);
	}
	//for array as well, course also need when multisort
	private function _cleanParams($params)
	{
		$cleanParams = array();
		if(is_array($params)){
			foreach($params as $param){
				$cleanParams[] = htmlspecialchars(trim($param));
			}
			return $cleanParams;
		}else{
			return htmlspecialchars(trim($params));
		}
	}

	public function deleteAction(){
		$id = $this->_getParam('id');
		if(!isset($id)){
			$this->_helper->redirector('index');
		}
		$request = $this->getRequest();
		if($request->isPost()){
			if($request->getPost('del') == 'yes'){
				$users = new Project_User_Table();
				$users->deleteUser($id);
				$this->_helper->redirector('index');
			}else{
				$this->_helper->redirector('index');
			}
		}else{
			$this->view->title = 'Delete User';
			$this->view->headTitle($this->view->title);
			$users = new Project_User_Table();
			$this->view->user = $users->getUser($id);
			$role = $users->getRoles($this->view->user->role_id);
			$this->view->userRole = $role->role;
		}
	}

	public function addAction(){
		$request = $this->getRequest();
		$form = new Application_Form_User();
		$form->getElement('passwFlag')->clearDecorators();
		$this->view->title = 'Add User';
		$this->view->headTitle($this->view->title);
		$this->view->form = $form;
		if($request->isPost()){
			$formData = $request->getPost();
			if($form->isValid($formData)){
				$user = new Project_User_Table();
				$username = $form->getValue('username');
				$password = $form->getValue('password');
				$role = $form->getValue('role_id');
				$user->addUser($username, $password, $role);
				$this->_helper->redirector('index');
			}else{
				$form->populate($formData);
			}
		}
	}
////////----------------bull shit!!!!!!! need to change
	public function editAction(){
		$request = $this->getRequest();
		$form = new Application_Form_User();
		//$form->getElement('password')->setAttrib('disabled',true);
		$this->view->title = 'Edit User';
		$this->view->headTitle($this->view->title);
		$this->view->form = $form;
		if($this->_getParam('passwFlag') == 0){
			$form->getElement('password')->setRequired(false);
		}
		if($request->isPost()){
			$formData = $request->getPost();
			if($form->isValid($formData)){
				$user = new Project_User_Table();
				$id = $this->_getParam('id');
//				$username = $form->getValue('username');
//				$password = $form->getValue('password');
//				$role_id = $form->getValue('role_id');
				$userAttributes = $this->_getDefeniteUserAttrs($form->getValues());
				$userAttributes['id'] = $id;
				$user->updateUser($userAttributes);
				$this->_helper->redirector('index');
			}else{
				$userAttributes = $this->_getDefeniteUserAttrs($formData);
				$form->setDefaults($userAttributes);
			}
		}else{
			$id = $this->_getParam('id',0);
			if($id > 0){
				$user = new Project_User_Table();
				$editedUser = $user->getUser($id);
				$form->setDefaults(array(
					'role_id'  => $editedUser->role_id,
					'username' => $editedUser->username,
				));
			}
		}
	}

//create an array containing defenite params for updating
//if flag is true while editing you can change a password
	private function _getDefeniteUserAttrs(array $postValues){
		$userAttributes = array();
		foreach($postValues as $attr=>$value){
			switch ($attr){
				case 'username':
					$userAttributes[$attr] = $value;
					break;
				case 'password':
					if($postValues['passwFlag'] != 0){
						$userAttributes[$attr] = $value;
					}
					break;
				case 'role_id':
					$userAttributes[$attr] = $value;
					break;
			}
		}
		return $userAttributes;
	}
}